package scut.facerecognition.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scut.facerecognition.entity.dto.PageResult;
import scut.facerecognition.entity.dto.PostDTO;
import scut.facerecognition.entity.pojo.Post;
import scut.facerecognition.result.Result;
import scut.facerecognition.service.PostService;

import java.util.HashMap;
import java.util.Map;

/**
 * 帖子相关接口
 */
@RestController
@RequestMapping("/api/posts")
public class PostController {
    
    @Autowired
    private PostService postService;
    
    /**
     * 帖子分页列表接口
     */
    @GetMapping
    public Result getPostList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "2") int sort) {
        
        Long userId = StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null;
        PageResult<PostDTO> pageResult = postService.getPostList(page, size, sort, userId);
        
        Map<String, Object> data = new HashMap<>();
        data.put("posts", pageResult.getRecords());
        data.put("total", pageResult.getTotal());
        data.put("pages", pageResult.getPages());
        
        return Result.success(data);
    }
    
    /**
     * 帖子详情接口
     */
    @GetMapping("/{id}")
    public Result getPostDetail(@PathVariable Long id) {
        Long userId = StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null;
        PostDTO postDTO = postService.getPostDetail(id, userId);
        
        if (postDTO == null) {
            return Result.error("帖子不存在或已删除");
        }
        
        return Result.success(postDTO);
    }
    
    /**
     * 发布帖子接口
     */
    @PostMapping
    public Result createPost(@RequestBody Post post) {
        // 验证用户是否登录
        StpUtil.checkLogin();
        
        // 设置用户ID
        post.setUserId(StpUtil.getLoginIdAsLong());
        
        // 创建帖子
        Long postId = postService.createPost(post);
        
        Map<String, Object> data = new HashMap<>();
        data.put("postId", postId);
        
        return Result.success(data);
    }
    
    /**
     * 更新帖子接口
     */
    @PutMapping("/{id}")
    public Result updatePost(@PathVariable Long id, @RequestBody Post post) {
        // 验证用户是否登录
        StpUtil.checkLogin();
        
        // 设置帖子ID和用户ID
        post.setId(id);
        post.setUserId(StpUtil.getLoginIdAsLong());
        
        // 更新帖子
        boolean success = postService.updatePost(post);
        
        if (!success) {
            return Result.error("更新失败，帖子可能不存在或无权限");
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("success", true);
        
        return Result.success(data);
    }
    
    /**
     * 删除帖子接口
     */
    @DeleteMapping("/{id}")
    public Result deletePost(@PathVariable Long id) {
        // 验证用户是否登录
        StpUtil.checkLogin();
        
        // 删除帖子
        boolean success = postService.deletePost(id, StpUtil.getLoginIdAsLong());
        
        if (!success) {
            return Result.error("删除失败，帖子可能不存在或无权限");
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("success", true);
        
        return Result.success(data);
    }
    
    /**
     * 点赞/取消点赞帖子接口
     */
    @PostMapping("/{id}/like")
    public Result togglePostLike(@PathVariable Long id) {
        // 验证用户是否登录
        StpUtil.checkLogin();
        
        // 点赞/取消点赞
        Object[] result = postService.togglePostLike(id, StpUtil.getLoginIdAsLong());
        
        Map<String, Object> data = new HashMap<>();
        data.put("liked", result[0]);
        data.put("likeCount", result[1]);
        
        return Result.success(data);
    }
}
